package com.corpgovernment.organization.Bo;

import com.corpgovernment.common.holder.TenantContextHolder;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.corpgovernment.common.utils.LogSplicingUtils;
import com.corpgovernment.supplier.constant.CommonConstant;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;


import java.util.Objects;

@Data
public class CommonSupplierUrlBo {

    private UrlInfo employeeOpenCard;

    private UrlInfo getOpenCardInfo;

    private UrlInfo getBindVerifyCode;

    private UrlInfo bindAccount;

    @Data
    public static class UrlInfo{
        private String name;
        private String url;
        private String corpId;
        private String subAccountName;
        private String supplierCode;
    }

    public static CommonSupplierUrlBo getCommonSupplierUrl(String json, String supplierCorpId, String supplierAccountName, StringBuilder logContext){
        if (Objects.isNull(logContext)){
            logContext = new StringBuilder();
        }
        LogSplicingUtils.addLogContext(logContext, "commonUrl：%s", json);
        if (StringUtils.isBlank(json)){
            LogSplicingUtils.addLogContext(logContext, "apollo未配置推送地址");
            return null;
        }
        //转换为note节点
        JsonNode jsonNode = JsonUtils.getJsonNode(json);
        //获取租户ID
        String tenantId = TenantContextHolder.getTenantId();
        LogSplicingUtils.addLogContext(logContext, "tenantId：%s", tenantId);

        //根据租户ID获取该租户配置的推送配置
        JsonNode path = jsonNode.findPath(tenantId);
        if (StringUtils.isBlank(path + "")){
            LogSplicingUtils.addLogContext(logContext, "apollo未配置该租户地址");
            return null;
        }
        //转换为实体类
        CommonSupplierUrlBo commonSupplierUrlBo = JsonUtils.parse(path.toString(), CommonSupplierUrlBo.class);

        commonSupplierUrlBo.getEmployeeOpenCard().setCorpId(supplierCorpId);
        commonSupplierUrlBo.getEmployeeOpenCard().setSubAccountName(supplierAccountName);
        commonSupplierUrlBo.getEmployeeOpenCard().setSupplierCode(CommonConstant.CTRIP_CODE);
        commonSupplierUrlBo.getBindAccount().setCorpId(supplierCorpId);
        commonSupplierUrlBo.getBindAccount().setSubAccountName(supplierAccountName);
        commonSupplierUrlBo.getBindAccount().setSupplierCode(CommonConstant.CTRIP_CODE);
        commonSupplierUrlBo.getGetBindVerifyCode().setCorpId(supplierCorpId);
        commonSupplierUrlBo.getGetBindVerifyCode().setSubAccountName(supplierAccountName);
        commonSupplierUrlBo.getGetBindVerifyCode().setSupplierCode(CommonConstant.CTRIP_CODE);
        commonSupplierUrlBo.getGetOpenCardInfo().setCorpId(supplierCorpId);
        commonSupplierUrlBo.getGetOpenCardInfo().setSubAccountName(supplierAccountName);
        commonSupplierUrlBo.getGetOpenCardInfo().setSupplierCode(CommonConstant.CTRIP_CODE);
        LogSplicingUtils.addLogContext(logContext, "commonSupplierUrlBo：%s", JsonUtils.toJsonString(commonSupplierUrlBo));
        return commonSupplierUrlBo;
    }
}
